/* eslint-disable @typescript-eslint/no-explicit-any */
import { isEqual } from 'lodash-es'

/**
 * 比较对象的新旧版本
 * 返回有更改的部分
 *
 * @param oldObj 旧数据
 * @param newObj 新数据
 */
function compareObject<T>(oldObj?: T, newObj?: T): Partial<T> | undefined
function compareObject(oldObj: any = {}, newObj: any = {}): any {
  const resObj: any = {}
  Object.keys(oldObj).forEach((key) => {
    if (!isEqual(oldObj[key], newObj[key])) {
      resObj[key] = newObj[key]
    }
  })
  if (Object.keys(resObj).length <= 0) return
  return resObj
}

export { compareObject }
